navigationGo.pngQuick Navigation
allprojects32.pngAll projects
hardware32.pngHardware
links32.pngLinks

favoriteStar32.pngTop projects
Alan numitron clock
Clapclap 2313/1386
SNES Pi Webserver
USB Volume/USB toys
Smokey amp
Laser cutter
WordClock
ardReveil v3
SNES Arcade cabinet
Game boy projects
cameleon
Home Presence Detector

github32.pngGitHub
AlanFromJapan

navigationMail.pngContact me

alanfjmail.png
3flags.pngWho's Alan?


Akizukidenshi
Elec-lab
Rand Nerd Tut
EEVblog
SpritesMods
AvrFreaks
Gameboy Dev
FLOZz' blog
Switch-science
Sparkfun
Suzusho
Datasheet Lib
Reddit Elec
Ermicro
Carnet du maker (fr)

misc arduino codes

Last update: Thu Jun 5 22:25:41 2025
kalshagar - Misc Arduino codes

function pointer

//signature definition
typedef void (* funcVoidInt)(uint16_t);
 
//affect
funcVoidInt vTheFunc = Func01;
 
//call
vTheFund(100);
NOTABENE: avoid using macro expanded types of arduino, better use the real underlying types such as uint16_t for int. Might help.

Change clock divider

Force (or not) the clock to run slower. Table with all values are in the doc of each chip so read it. Here's the code, put it in the setup()/main().
    //factor settings is to divide internal clock 8MHz by 8.
    //don't, and just run at 8 MHz (set the clock divider to 1 so no effect)
    CLKPR = (1<<CLKPCE);
    CLKPR = 0; // Divide by 1 

Randow number generator init

Just increase a figure stored in EEPROM and init with it at startup. So easy and so obvious: so smart in onw word.
http://www.fangletronics.com/2010/02/amazing-dr-boardmans-colour-conundrum.html
#include <avr/eeprom.h>
/*
 * Use a variable stored in EEPROM to ensure the random color
 * sequence changes from one game to the next.
 */
void initRand()
{
    uint8_t vSeed = eeprom_read_word(0); // load last stored seed
    srand(%20%20vSeed); // increment and use value as seed
    eeprom_write_word(0, vSeed); //store the new seed for next time
}
All content on this site is shared under the MIT licence (do what u want, don't sue me, hat tip appreciated)
electrogeek.tokyo ~ Formerly known as Kalshagar.wikispaces.com and electrogeek.cc (AlanFromJapan [2009 - 2025])